#pragma once 

#include <iostream>
#include "sock.hpp"
#include "log.hpp"
#include <algorithm>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>


const uint16_t default_port = 8080;
const int Fd_Max_Num = 1024;
const int default_fd = -1;

class SelectServer
{
public:
    SelectServer(uint16_t port = default_port)
        : _port(port)
    {
        for(int i = 0; i < Fd_Max_Num; i++)
        {
            _fd_array[i] = default_fd;
        }
    }

    ~SelectServer()
    {
        _listen_sock.Close();
    }

    void ServerInit()
    {
        _listen_sock.Socket();
        _listen_sock.Bind(_port);
        _listen_sock.Listen();
    }

    void Print()
    {
        for(int i = 0; i < Fd_Max_Num; i++)
        {
            if(_fd_array[i] == default_fd)
                continue;
            std::cout << _fd_array[i] << " ";
        }
        std::cout << std::endl;
    }

    void Accept()
    {
        std::string client_ip;
        uint16_t client_port;
        int fd = _listen_sock.Accept(client_ip, client_port);
        if(fd < 0)
        {
            std::cout << "accept error" << std::endl;
            return;
        }
        
        int pos = 0;
        for(; pos < Fd_Max_Num; pos++)
        {
            if(_fd_array[pos] != default_fd)
                continue;
            else
                break;
        }
        if(pos == Fd_Max_Num)
        {
            std::cout << "select 等待的文件描述符的数量已满" << std::endl;
            return;
        }
        else
        {
            _fd_array[pos] = fd;
            Print();
        }

    }

    void Read(int fd, int pos)
    {
        char buffer[1024];
        ssize_t n = read(fd, buffer, sizeof(buffer) - 1);
        if(n < 0)
        {
            std::cout << "读取失败" << std::endl;
            return;
        }
        else if(n > 0)
        {
            buffer[n] = '\0';
            std::cout << "message : " << buffer << std::endl;
        }
        else
        {
            std::cout << "文件关闭" << std::endl;
            _fd_array[pos] = default_fd;
            close(fd);
        }
    }

    void handler(fd_set* rfds)
    {
        // 遍历 看哪一个文件描述符好了
        for(int i = 0; i < Fd_Max_Num; i++)
        {
            int fd = _fd_array[i];
            if(FD_ISSET(fd, rfds))
            {
                if(fd == _listen_sock.GetSockfd())
                {
                    // 处理新链接
                    Accept();
                }
                else
                {
                    // 读取数据
                    Read(fd, i);
                }
            }
        }
    }


    void Run()
    {
        _fd_array[0] = _listen_sock.GetSockfd();
        while(1)
        {
            fd_set rfds;
            FD_ZERO(&rfds);
            int max_sock = _fd_array[0];

            // 输入输出型参数 ： 每次需要重新设置
            struct timeval timeout = { 1, 0 };
            // int listen_sock = _listen_sock.GetSockfd();

            // 遍历 fd_array 表 填写 rfds 获取max_sock
            for(int i = 0; i < Fd_Max_Num; i++)
            {
                if(_fd_array[i] == default_fd)
                    continue;

                FD_SET(_fd_array[i], &rfds); 
                max_sock = std::max(_fd_array[i], max_sock);
            }
            std::cout << "max sock " << max_sock << std::endl;


            int n = select(max_sock + 1, &rfds, nullptr, nullptr, /*&timeout*/ &timeout);
            if(n > 0)
            {
                // 处理
                std::cout << "资源就绪， 处理ing" << std::endl;
                handler(&rfds);
            }
            else if(n < 0)
            {
                std::cout << "select error " << std::endl;
            }
            else if(n == 0)
            {
                // std::cout << "timeout : " << timeout.tv_sec << " " << timeout.tv_usec << std::endl;
                std::cout << "n == 0, 资源没有就绪" << std::endl; 
            }
        }
    }
private:
    uint32_t _port = 8080;
    Sock _listen_sock;
    int _fd_array[Fd_Max_Num];

};